using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;
using DevExpress.Accessibility;
using DevExpress.Utils.Editors;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Drawing;
using DevExpress.XtraEditors.Registrator;
using DevExpress.XtraEditors.Repository;

namespace Comteck.Winforms.Controls {
  /// <summary>
  /// 
  /// </summary>
  [ToolboxItem(true)]
  public class MyButtonEdit : ButtonEdit {

    private bool isSelectAll = false;

    static MyButtonEdit() {
      RepositoryItemMyButtonEdit.Register();
    }

    /// <summary>
    /// 
    /// </summary>
    public MyButtonEdit() : base() {
      Initialize();

      Enter += this.MyButtonEdit_Enter;
      MouseUp += this.MyButtonEdit_MouseUp; ;

      this.Properties.AllowMouseWheel = false;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void MyButtonEdit_Enter(object sender, EventArgs e) {
      // 设置全选标记
      isSelectAll = true;
      this.SelectAll();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void MyButtonEdit_MouseUp(object sender, MouseEventArgs e) {
      // 如果鼠标左键操作并且标记存在，则执行全选
      if (e.Button == MouseButtons.Left && isSelectAll) {
        this.SelectAll();
      }

      // 取消全选标记
      isSelectAll = false;
    }

    /// <summary>
    /// 初始化界面
    /// </summary>
    private void Initialize() {
    }


    #region fields

    /// <summary>
    /// 
    /// </summary>
    public override string EditorTypeName => RepositoryItemMyButtonEdit.EditorName;

    /// <summary>
    /// 
    /// </summary>
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public new RepositoryItemMyButtonEdit Properties => base.Properties as RepositoryItemMyButtonEdit;

    //
    // 摘要:
    //     Gets or sets whether focus is moved to the next control (according to the tab
    //     order) when an end-user presses ENTER.
    [DefaultValue(true)]
    [DXCategory("Behavior")]
    public override bool EnterMoveNextControl { get; set; } = true;

    #endregion
  }

  /// <summary>
  /// 
  /// </summary>
  [DXCategory("Properties")]
  [UserRepositoryItem("Register")]
  [ToolboxItem(true)]
  public class RepositoryItemMyButtonEdit : RepositoryItemButtonEdit {

    /// <summary>
    /// 
    /// </summary>
    static RepositoryItemMyButtonEdit() {
      Register();
    }

    /// <summary>
    /// 
    /// </summary>
    public RepositoryItemMyButtonEdit() {
    }

    /// <summary>
    /// 
    /// </summary>
    internal const string EditorName = nameof(MyButtonEdit);

    /// <summary>
    /// 
    /// </summary>
    public override string EditorTypeName => EditorName;

    //
    // 摘要:
    //     Gets or sets whether value scrolling via the Mouse Wheel is supported by the
    //     editor.
    [Browsable(false)]
    [DefaultValue(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    [DXCategory("Behavior")]
    [EditorBrowsable(EditorBrowsableState.Never)]
    public override bool AllowMouseWheel { get; set; } = false;

    /// <summary>
    /// 
    /// </summary>
    public static void Register() {
      EditorRegistrationInfo.Default.Editors.Add(
        new EditorClassInfo(
          EditorName, // 
          typeof(MyButtonEdit),
          typeof(RepositoryItemMyButtonEdit),
          typeof(DevExpress.XtraEditors.ViewInfo.ButtonEditViewInfo),
          new ButtonEditPainter(),
          true,
          null,
          typeof(ButtonEditAccessible)));
    }
  }

}
